// then -> generator * yield ->co->async  await
//async/await 手写
function asyncToGenerator(generatorFunc){
  return function (){
    const gen = generatorFunc.apply(this,arguments)
    return new Promise((resolve,reject)=>{
        function setup(key,arg){
          let generatorResult;
          try{
           generatorResult = gen[key](arg)
          }catch(error){
            return reject(error)
          }
          const {value,done} = generatorResult
          if(done){
            return resolve(value).then(
                val=>setup('next',val),
                err=>setup('throw',err)
            )
          }else{
            return Promise.resolve
          }
        }
        setup('next')
    })
  }
}

function* testGenerator(){
    const result1 = yield Promise.resolve('hello')
    console.log(result1)
    const result2 = yield Promise.resolve('world')
    console.log(result2);
    return 'done'
}

const asyncFunc = asyncToGenerator(testGenerator);
asyncFunc()
.then(finalResult=>{
    console.log('最终结果',finalResult)
})